package com.fanfanwang.ffw.app.common;


import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Util {


    /**
     * md5加密
     * @param str 
     * @return 加密后的md5字符
     */
    public static String getMD5Str(String str) {
        MessageDigest messageDigest = null;

        try {
            messageDigest = MessageDigest.getInstance("MD5");

            messageDigest.reset();

            messageDigest.update(str.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            System.out.println("NoSuchAlgorithmException caught!");
            System.exit(-1);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        byte[] byteArray = messageDigest.digest();
        /*
        StringBuffer md5StrBuff = new StringBuffer();

        for (int i = 0; i < byteArray.length; i++) {
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
                md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
            } else {
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
            }
        }
        // 16位加密，从第9位到25
        return md5StrBuff.substring(8, 24).toString().toUpperCase();
        */
        return byte2hex(byteArray);
    }
    
    public static String getMD5To32(String sourceStr) {
        String result = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(sourceStr.getBytes("UTF-8"));
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString();
            System.out.println("MD5(" + sourceStr + ",32) = " + result);
            System.out.println("MD5(" + sourceStr + ",16) = " + buf.toString().substring(8, 24));
        } catch (NoSuchAlgorithmException e) {
            System.out.println(e);
        } catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
    }
    public static String getMD5To16(String sourceStr) {
    	String result = "";
    	try {
    		MessageDigest md = MessageDigest.getInstance("MD5");
    		md.update(sourceStr.getBytes("UTF-8"));
    		byte b[] = md.digest();
    		int i;
    		StringBuffer buf = new StringBuffer("");
    		for (int offset = 0; offset < b.length; offset++) {
    			i = b[offset];
    			if (i < 0)
    				i += 256;
    			if (i < 16)
    				buf.append("0");
    			buf.append(Integer.toHexString(i));
    		}
    		result = buf.toString().substring(8, 24);
    		System.out.println("MD5(" + sourceStr + ",32) = " + result);
    		System.out.println("MD5(" + sourceStr + ",16) = " + buf.toString().substring(8, 24));
    	} catch (NoSuchAlgorithmException e) {
    		System.out.println(e);
    	} catch (UnsupportedEncodingException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    	return result;
    }


    /**
     *  二行制转字符
     * @param b
     * @return
     */
    public static String byte2hex(byte[] b)
    {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0XFF);
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
//        return hs.toUpperCase();
        return hs.toLowerCase();
    }

}
